import gdp.stdlib.StdAudio;
import gdp.stdlib.StdDraw;
import gdp.stdlib.StdIn;

import java.util.Arrays;

public class Sonnensystem {

	public static void main(String[] args) {

		// System.setIn(new FileInputStream("planets.txt"));

		int N = StdIn.readInt();
		double R = StdIn.readDouble();

		double G = 6.67E-11;

		double[] rx = new double[N];
		double[] ry = new double[N];
		double[] vx = new double[N];
		double[] vy = new double[N];
		double[] M = new double[N];
		double[] forcex = new double[N];
		double[] forcey = new double[N];
		String[] File = new String[N];
		String background = "starfield.jpg";
		String backaudio = "2001.mid";
		
		for (int i = 0; i < N; i++) {
			rx[i] = StdIn.readDouble();
			ry[i] = StdIn.readDouble();
			vx[i] = StdIn.readDouble();
			vy[i] = StdIn.readDouble();
			M[i] = StdIn.readDouble();
			File[i] = StdIn.readString();
		}
		
		// StdOut.println("N:" + N);
		// StdOut.println("R:" + R);
		// for (int i = 0; i < N; i++) {
		// StdOut.println("" + i + ": (" + rx[i] + "," + ry[i] + ") v: (" +
		// vx[i] + "," + vy[i] + ") m:" + m[i] + " file:" + file[i]);
		// }

		int dt = 25000;

		StdDraw.setXscale(-R, R);
		StdDraw.setYscale(-R, R);
		StdAudio.play(backaudio);

		while (true) {

			// output

			StdDraw.picture(-1, -1, background);
			for (int i = 0; i < N; i++) {
				StdDraw.picture(rx[i], ry[i], File[i]);
			}
			StdDraw.show(10);

			// reset interval data
			Arrays.fill(forcex, 0.0);
			Arrays.fill(forcey, 0.0);
			
			// calculate resulting forces for every object

			for (int i = 0; i < (N - 1); i++) {

				// StdOut.print("\n i: " + i + " " + file[i]);
				// StdOut.printf(" r: ( %1.3e,%1.3e)", rx[i], ry[i]);
				
				// calculate influencing forces of other objects
				for (int k = i + 1; k < N; k++) {

					// distance between i and k
					double dx = (rx[k] - rx[i]);
					double dy = (ry[k] - ry[i]);
					double d2 = (dx * dx) + (dy * dy);
					double d = Math.sqrt(d2);

					// StdOut.printf(" \n   nach %d d: ( %1.3e,%1.3e) = %1.3e",
					// k, dx, dy, d);

					// force
					double f = (G * M[i] * M[k]) / d2;
					double fx = ((f * dx) / d);
					double fy = ((f * dy) / d);

					forcex[i] = forcex[i] + fx;
					forcey[i] = forcey[i] + fy;
					forcex[k] = forcex[k] - fx;
					forcey[k] = forcey[k] - fy;

					// StdOut.printf(" ft: ( %1.3e)", f);
					// StdOut.printf(" f: ( %1.3e,%1.3e)", fx, fy);

				}

			}

			for (int i = 0; i < N; i++) {
				// StdOut.print("\n i: " + i + " " + file[i]);
				// new v
				vx[i] = vx[i] + ((dt * forcex[i]) / M[i]);
				vy[i] = vy[i] + ((dt * forcey[i]) / M[i]);
				// StdOut.printf(" v: ( %1.3e,%1.3e)", vx[i], vy[i]);

				// new coo
				rx[i] = rx[i] + (dt * vx[i]);
				ry[i] = ry[i] + (dt * vy[i]);
				// StdOut.printf(" r: ( %1.3e,%1.3e)", rx[i], ry[i]);

			}
		}

	}
}
